package com.quickaction;

import android.content.Context;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;

import android.widget.PopupWindow;

/**
 * This class does most of the work of wrapping the {@link PopupWindow} so it's simpler to use. 
 * Edited by Lorensius. W. L. T
 * 
 * @author qberticus
 * 
 */
public class CustomPopupWindow {
        protected final View anchor;
        protected final PopupWindow window;
        private View root;
        private Drawable background = null;
        protected final WindowManager windowManager;

        /**
         * Create a QuickAction
         * 
         * @param anchor
         *            the view that the QuickAction will be displaying 'from'
         */
        public CustomPopupWindow(View anchor) {
                this.anchor = anchor;
                this.window = new PopupWindow(anchor.getContext());
                this.windowManager = (WindowManager) anchor.getContext().getSystemService(Context.WINDOW_SERVICE);

                onCreate();
        }

        public void dismiss() {
                window.dismiss();
        }
        
        /**
         * Anything you want to have happen when created. Probably should create a view and setup the event listeners on
         * child views.
         */
        protected void onCreate() {}

        /**
         * In case there is stuff to do right before displaying.
         */
        protected void onShow() {}

        protected void preShow() {
                if (root == null) {
                        throw new IllegalStateException("setContentView was not called with a view to display.");
                }

                onShow();

                // if using PopupWindow#setBackgroundDrawable this is the only values of the width and height that make it work
                // otherwise you need to set the background of the root viewgroup
                // and set the popupwindow background to an empty BitmapDrawable
                if (background == null) {
                        window.setBackgroundDrawable(new BitmapDrawable());
                } else {
                        window.setBackgroundDrawable(background);
                }
                
                window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                window.setTouchable(true);
                window.setFocusable(true);

                window.setContentView(root);
        }

        public void setBackgroundDrawable(Drawable background) {
                this.background = background;
        }

        /**
         * Sets the content view. Probably should be called from {@link onCreate}
         * 
         * @param root
         *            the view the popup will display
         */
        public void setContentView(View root) {
                this.root = root;

                window.setContentView(root);
        }

        /**
         * Will inflate and set the view from a resource id
         * 
         * @param layoutResID
         */
        public void setContentView(int layoutResID) {
                LayoutInflater inflator =
                                (LayoutInflater) anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                setContentView(inflator.inflate(layoutResID, null));
        }

        /**
         * If you want to do anything when {@link dismiss} is called
         * 
         * @param listener
         */
        public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
                window.setOnDismissListener(listener);
        }
        
        /**
         * If you want to block outside touch dismiss
         */
        public void setOutsideTouchBlock() {
                window.setTouchInterceptor(new OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                if ((event.getAction() == MotionEvent.ACTION_DOWN) && ((event.getY() < v.getTop() || (event.getY() > v.getBottom())))) {
                                        return true;
                                }
                                return false;
                        }
                });
        }
}